<?php

class QImage extends QObject 
{
	private $path = null;
	
	public function __construct($path = null)
	{
		$this->path = $path;
	}
	
	private function createFromType($type, $path = null) 
	{
		if (!$path)
			$path = $this->path;
		
		$im = null;
		switch ($type) {
			case 1:
			{
				$im = ImageCreateFromGif($path);
				break;
			}
			case 2:
			{
				$im = ImageCreateFromJpeg($path);
				break;
			}
			case 3:
			{
				$im = ImageCreateFromPNG($path);
				break;
			}
			default:
			{
				throw new Exception("Unknown image type [$type] for file : {$path}");
				break;
			}
		}
		return $im;
	}
	
	private function saveImage($type, $im, $path, $quality = 100, $to_file = true) 
	{
		$res = null;

		if (!function_exists('imagegif')) $type = 3;

		switch ($type)
		{
			case 1:
			{
				$res = ImageGIF($im, $path);
				break;
			}
			case 2:
			{
				$res = ImageJPEG($im, $path, $quality);
				break;
			}
			case 3:
			{
				if (PHP_VERSION >= '5.1.2')
				{
					$quality = 9 - min( round($quality / 10), 9 );
					$res = ImagePNG($im, $path, $quality);
				}
				else
					$res = ImagePNG($im, $path);
					
				break;
			}
			default:
			{
				throw new Exception("Unknown image type [$type] for file : {$path}");
			}
	    }
	}
	
	public static function GetThumbnail($path, $width = 150, $height = 150, $trim_canvas = false, 
		$fill_color = false, $force_create = false, $default_image = "images/na.gif",
		$color_fill = array (255, 255, 255), $trim_position = "center center")
	{
		$img = new QImage($path);
		$thumbpath = $img->makeThumbnail($width, $height, $trim_canvas, $fill_color, $force_create, 
				$default_image, $color_fill, $trim_position);
		
		// echo "$path => $thumbpath \n<br/>";
		return $thumbpath;
	}
	
	public static function GetThumbnailPath($path, $width = 150, $height = 150)
	{
		if ((!$path) || ((strlen(trim($path)) < 1)))
			return null;
		return QWebApp::GetTempPath() . "thumbs/{$width}x{$height}/" . ltrim($path, "\\/");
	}
	
	public function makeThumbnail($width = 150, $height = 150, $trim_canvas = false, 
		$fill_color = true, $force_create = false, $default_image = "images/na.gif",
		$color_fill = array (255, 255, 255), $trim_position = "center center")
	{
		$file = basename($this->path);
		$folder = QWebApp::GetTempPath() . "thumbs/{$width}x{$height}/" . trim(dirname($this->path), "\\/") . "/";
		$path = $this->path;
		
		$thumb_path = $folder . $file;
		
		if (file_exists($thumb_path) && (!$force_create))
			return $thumb_path;
		else if ((strlen(trim($this->path)) < 1) || (!is_file($this->path)))
		{
			$file = basename($default_image);
			$folder = QWebApp::GetTempPath() . "thumbs/{$width}x{$height}/" . trim(dirname($default_image), "\\/") . "/";
			$path = $default_image;
			
			$thumb_path = $folder . $file;
		}

		// $fill_color has priority
		if ($fill_color)
			$trim_canvas = false;

		list($orig_width, $orig_height, $type, $attr) = getimagesize($path);
		$orig_img_path = $path;

		if (($orig_width == $width) && ($orig_height == $height))
			return $orig_img_path;

		if (($type < 1) || ($type > 3))
			throw new Exception("Unknown file type [$type] for [{$path}]");

		$img = $this->createFromType($type, $path);

		$new_width = $orig_width;
		$new_height = $orig_height;

		$per_x = $new_width / $width;
		$per_y = $new_height / $height;

		$pos_x = 0;
		$pos_y = 0;
		if ($trim_canvas)
		{
			
			$use_per = $per_y;
			if ($per_y < $per_x)
			{
				$new_width = $new_width / $per_y;
				$new_height = $new_height / $per_y;
				$use_per = $per_y;
			}
			else
			{
				$new_width = $new_width / $per_x;
				$new_height = $new_height / $per_x;
				$use_per = $per_x;
			}

			list($trim_pos_x, $trim_pos_y) = explode(" ", $trim_position, 2);
			
			$pos_x = ($orig_width - $width*$use_per) / 2;
			$pos_y = ($orig_height - $height*$use_per) / 2;
			
			// var_dump($height, $new_height);

			$canvas_new_width = $width;
			$canvas_new_height = $height;
		}
		else
		{
			if (($new_width >= $width) || ($new_height >= $height))
			{
				$use_per = $per_y;
				if ($per_y > $per_x)
				{
					$new_width = $orig_width / $per_y;
					$new_height = $orig_height / $per_y;
					$use_per = $per_y;
				}
				else
				{
					$new_width = $orig_width / $per_x;
					$new_height = $orig_height / $per_x;
					$use_per = $per_x;
				}

				$pos_x = 0;
				$pos_y = 0;
			}
		}

		// if the original dimensions are smaller than the target ones,
		// the picture is centered and filled with $fillcolor
		if (($new_width > $orig_width) || ($new_height > $orig_height))
		{
			$fill_color = true;
			$trim_canvas = false;
			if ($new_width > $orig_width)
			{
				$pos_x = ($new_width - $orig_width) / 2;
				$new_width = $orig_width;
			}
			if ($new_height > $orig_height)
			{
				$pos_y = ($new_height - $orig_height) / 2;
				$new_height = $orig_height;
			}
		}
		else if ($fill_color)
		{
			$pos_x = abs(($width - $new_width) / 2);
			$pos_y = abs(($height - $new_height) / 2);
		}

		if ($type == 1)
		{
			if ($trim_canvas || $fill_color)
				$ni = imagecreate($width, $height);
			else
				$ni = imagecreate($new_width, $new_height);
		}
		else
		{
			if ($trim_canvas || $fill_color)
				$ni = ImageCreateTrueColor($width, $height);
			else
				$ni = ImageCreateTrueColor($new_width, $new_height);
		}

		$white = imagecolorallocate($ni, $color_fill[0], $color_fill[1], $color_fill[2]);

		if ($trim_canvas || $fill_color)
			imagefilledrectangle($ni, 0, 0, $width, $height, $white);
		else
			imagefilledrectangle($ni, 0, 0, $new_width, $new_height, $white);
		imagepalettecopy($ni, $img);

		//echo "[$width][$height][".($pos_x)."][".($pos_y)."] | ";
		if ($trim_canvas)
		{
			// echo "trim canvas"
			imagecopyresampled(
				$ni, $img,
				0, 0, $pos_x, $pos_y,
				$width, $height,
				$orig_width - (2 * $pos_x), $orig_height - (2 * $pos_y));
		}
		else
		{
			imagecopyresampled(
				$ni, $img,
				$pos_x, $pos_y, 0, 0,
				$new_width, $new_height,
				$orig_width, $orig_height);
		}
		@imagedestroy($img);

		if (!is_dir($folder))
			@mkdir ($folder, 0777, true);

		$this->saveImage($type, $ni, $thumb_path);

		try
		{
			if (file_exists($thumb_path))
			{
				$usr_id = posix_getuid();
				if ($usr_id == fileowner($thumb_path))
					@chmod($thumb_path, 0755);
			}
		}
		catch (Exception $ex)
		{
			//
		}
		@imagedestroy($ni);

		return $thumb_path;
	}
}

?>